package com.xyht.sca_s.student_manage_system.modules.personnelManagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.SmsBirthdayReminder;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsBirthdayReminderAddReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsBirthdayReminderDeletedReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsBirthdayReminderUpdateReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.req.SmsTeacherInfoUpdateBirthReq;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.entity.resp.SmsBirthdayReminderResp;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.mapper.SmsBirthdayReminderMapper;
import com.xyht.sca_s.student_manage_system.modules.personnelManagement.service.SmsBirthdayReminderService;
import com.xyht.sca_s.student_manage_system.modules.teacher.entity.SmsTeacherInfo;
import com.xyht.sca_s.student_manage_system.modules.teacher.mapper.SmsTeacherInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 生日提醒 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-09-25
 */
@Service
public class SmsBirthdayReminderServiceImpl extends ServiceImpl<SmsBirthdayReminderMapper, SmsBirthdayReminder> implements SmsBirthdayReminderService {

    @Autowired
    private SmsBirthdayReminderMapper birthdayReminderMapper;
    @Autowired
    private SmsTeacherInfoMapper teacherInfoMapper;

    @Override
    @Transactional
    public ResponseResult updateTchBirth(SmsTeacherInfoUpdateBirthReq updateBirthReq) {
        if(isNullOrEmpty(updateBirthReq.getTchId(),updateBirthReq.getTchBirth())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsTeacherInfo teacherInfo = teacherInfoMapper.selectById(updateBirthReq.getTchId());
        if(isNullOrEmpty(teacherInfo)){
            return CommonResult.failed(CommonCodeEnum.TEACHER_NOT_EXIST);
        }
        teacherInfo.setTchBirth(updateBirthReq.getTchBirth());
        teacherInfoMapper.updateById(teacherInfo);
        return CommonResult.success();
    }

    @Override
    @Transactional
    public ResponseResult addBirthdayReminder(SmsBirthdayReminderAddReq addReq) {
        if(isNullOrEmpty(addReq.getBirthMessages())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //先删除数据
        birthdayReminderMapper.delete(null);
        //再新增数据
        SmsBirthdayReminder addBirthdayReminder = new SmsBirthdayReminder();
        BeanUtils.copyProperties(addReq,addBirthdayReminder);
        birthdayReminderMapper.insert(addBirthdayReminder);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getBirthdayReminder() {
        SmsBirthdayReminderResp resp = new SmsBirthdayReminderResp();
        List<SmsBirthdayReminder> birthdayReminderList = birthdayReminderMapper.selectList(null);
        if(birthdayReminderList == null || birthdayReminderList.size() < 1){
            return CommonResult.success(resp);
        }
        SmsBirthdayReminder birthdayReminder = birthdayReminderList.get(0);
        BeanUtils.copyProperties(birthdayReminder,resp);

        return CommonResult.success(resp);
    }

    @Override
    public ResponseResult updateBirthdayReminder(SmsBirthdayReminderUpdateReq updateReq) {
        if(isNullOrEmpty(updateReq.getId(),updateReq.getBirthMessages())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        String birthDayReminderId = updateReq.getId();
        SmsBirthdayReminder birthdayReminder = birthdayReminderMapper.selectById(birthDayReminderId);
        if(isNullOrEmpty(birthdayReminder)){
            return CommonResult.failed(CommonCodeEnum.BIRTHDAY_REMINDER_NOT_EXIST);
        }
        SmsBirthdayReminder updateBirthdayReminder = new SmsBirthdayReminder();
        BeanUtils.copyProperties(updateReq,updateBirthdayReminder);
        birthdayReminderMapper.updateById(updateBirthdayReminder);
        return CommonResult.success();
    }

    @Override
    public ResponseResult deletedBirthdayReminder(SmsBirthdayReminderDeletedReq deletedReq) {
        if(isNullOrEmpty(deletedReq.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsBirthdayReminder birthdayReminder = birthdayReminderMapper.selectById(deletedReq.getId());
        if(isNullOrEmpty(birthdayReminder)){
            return CommonResult.failed(CommonCodeEnum.BIRTHDAY_REMINDER_NOT_EXIST);
        }
        birthdayReminderMapper.deleteById(deletedReq.getId());
        return CommonResult.success();
    }
}
